﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpriteSheetRuntime;
using System.IO;

namespace Megaman
{
    
    class Stage
    {
        public string Name;
        public string levelBasePath;
        private int levelIndex = -1;
        public Level level;
        private IServiceProvider Services;
        public List<Boss> bosses;
        public List<Cutscene> cutscenes;
        public List<Stage> nextStages;
        public bool Completed = false;
        public bool Available = false;
        // Find the path of the next level.
        public string levelPath;
        StageManager stageManager;
        public Texture2D icon;
        public Texture2D iconSelected;

        public int Lives = 3;
        

        public ContentManager Content
        {
            get{ return content; }
        }
        ContentManager content;
        


        public Stage(IServiceProvider service, String name, String path, StageManager stageManager)
        {
            Services = service;
            Name = name;
            this.stageManager = stageManager;
            this.levelBasePath = path;
            bosses = new List<Boss>();
            nextStages = new List<Stage>();
            content = new ContentManager(service, "Content");
            icon = content.Load<Texture2D>(path + "/icon");
            iconSelected = content.Load<Texture2D>(path + "/icon-selected");
         
            
        }

        public void addBoss(Boss boss)
        {
            bosses.Add(boss);
        }

        public void deleteBoss(int index)
        {
            bosses.RemoveAt(index);
        }

        public void LoadNextLevel()
        {

            // Loop here so we can try again when we can't find a level.
            while (true)
            {
                // Try to find the next level. 
                levelPath = String.Format(levelBasePath + "/Levels/{0}.txt", ++levelIndex);
                levelPath = Path.Combine(StorageContainer.TitleLocation, "Content/" + levelPath);

                if (File.Exists(levelPath))
                    break;

                // If there isn't even a level 0, something has gone wrong.
                if (levelIndex == 0)
                    throw new Exception("No levels found.");

                // Whenever we can't find a level, start over again at 0.
                levelIndex = -1;
            }

            // Unloads the content for the current level before loading the next one.
            if (level != null)
                level.Dispose();


            // Load the level.
            level = new Level(Services, levelBasePath, levelPath, this);
        }

        public void ReloadLevel()
        {
          levelIndex--;
          LoadNextLevel();

        }
        public void Update(GameTime gameTime)
        {
            HandleInput();
            level.Update(gameTime);
        }

        private void HandleInput()
        {
            
        }

        public void ExitedLevel()
        {
            level.Dispose();
            stageManager.UnloadStage();
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            level.Draw(gameTime, spriteBatch);
        }
    }
}
